<html>
<head>
    <meta charset="UTF-8">
    <title>患者信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
</head>
<body>
<div class="demoTable">
    <div class="layui-form" style="text-align: center">
        <div class="layui-input-inline">
            <select name="hospital.hid" id="hid">
                <option value="">---请选择医院---</option>
            </select>
        </div>
        <div class="layui-inline">
            <input class="layui-input" name="pname" id="pname" placeholder="姓名" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input class="layui-input" name="pphone" id="pphone" placeholder="手机号" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input class="layui-input" name="puser" id="puser" placeholder="编号" autocomplete="off">
        </div>
        <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
    </div>
</div>
<button class="layui-btn layui-btn-sm" lay-event="refresh" id="flush"><i class="layui-icon layui-icon-refresh-3"></i></button>
<table class="layui-hide" id="demo" lay-filter="test"></table>
<!-- 自定义的控件 -->
<div id="toolbar" style="display: none; ">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add" data-method="notice" data-type="auto">添加患者</button>
    </div>
</div>
<script type="text/html" id="buttonTpl">
    {{#  if(d.pstatus == 0){ }}
    <button class="layui-btn layui-btn-xs">正常</button>
    {{#  } else { }}
    <button class="layui-btn layui-btn-primary layui-btn-xs">废弃</button>
    {{#  } }}
</script>
<script type="text/jsp" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">恢复</a>
</script>
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    //医院下拉款
    layui.use(['form','laydate'], function() {
        var form = layui.form;
        $.ajax({
            url: "/hospital/findAll",
            type: "post",
            // data:{},
            dataType: "json",
            success: function (res) {
                console.log(res)
                $.each(res.date.list, function (i, obj) {
                    console.log(obj);
                    $("#hid").append('<option value="' + obj.hid + '">' + obj.hname + '</option>');
                });
                form.render('select'); //刷新select选择框渲染
            }
        });
    });
    layui.use(['table', 'layer', 'jquery'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        //第一个实例
        table.render({
            elem: '#demo'
            , height: document.body.clientHeight - 180
            , url: '/patient/showhospital2' //数据接口
            , page: true
            , id: 'testReload'
            , cols: [[ //表头
                {field: 'pid', title: 'ID',sort: true}
                , {field: 'puser', title: '用户编号', sort: true}
                , {field: 'role', title: '角色', sort: true,
                    templet: function (d) {
                        console.log(d);
                        return d.role.roleType
                    }
                }
                , {field: 'hospital', title: '医院名称', sort: true,
                    templet: function (d) {
                        console.log(d)
                        return d.hospital.hname
                    }
                }
                , {field: 'doctor', title: '治疗医生', sort: true,
                    templet: function (d) {
                        console.log(d)
                        return d.doctor.dname
                    }
                }
                , {field: 'files', title: '临床诊断', sort: true,
                    templet: function (d) {
                        console.log(d)
                        if(d.files.uposition == 2){
                            if(d.files.cname != null){
                                return d.files.cname;
                            }else {
                            }
                        }
                        return "暂无临床诊断";
                    }
                }
                , {field: 'files', title: '检测项目', sort: true,
                    templet: function (d) {
                        console.log(d)
                        if(d.files.uposition == 0){
                            if(d.files.cname != null){
                                return d.files.cname;
                            }else {
                            }
                        }
                        return "暂无检测项目";
                    }}
                , {field: 'files', title: '检测结果', sort: true,
                    templet: function (d) {
                        console.log(d)
                        if(d.files.uposition == 1){
                            if(d.files.cname != null){
                                return d.files.cname;
                            }else {
                            }
                        }
                        return "暂无检测结果";
                    }}
                , {field: 'pphone', title: '账号', sort: true}
                , {field: 'pname', title: '姓名', sort: true}
                , {field: 'psex', title: '性别', sort: true}
                , {field: 'ptime', title: '注册时间', sort: true,
                    templet: "<div>{{layui.util.toDateString(d.ptime, 'yyyy-MM-dd')}}</div>"
                }
                , {field: 'pstatus', title: '状态', sort: true, templet: '#buttonTpl', align: 'center'}
                , {field: 'right', title: '操作', toolbar: '#barDemo'}
            ]]
            , toolbar: '#toolbar',//表格头信息
            parseData: function (rs) {//数据格式解析
                console.log(rs);
                if (rs.code == 200) {
                    return {
                        "code": rs.code,
                        "msg": rs.msg,
                        "count": rs.date.total,
                        "data": rs.date.list
                    }
                }
            },
            response: {	// 设置响应码
                statusCode: 200 //规定成功的状态码，默认：0
            }
        });
        //刷新事件
        $(document).on('click',"#flush",function(){
            //parent.location.reload(); //重新加载父页面
            location.reload();//重新加载当前页面
        });
        //监听事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    open("添加页面", "/view/patient/add.html");
                    break;
            }
            ;
        });
        //监听行事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                open("详情页面", "/view/patient/detail.html?pid=" + data.pid);
            } else if (obj.event === 'del') {
                layer.confirm('真的恢复此患者信息吗？', function (index) {
                    $.ajax({
                        type: "POST",
                        url: "/patient/recover",
                        data: {pid: data.pid},
                        dataType: "json",
                        success: function (da) {
                            if (da.code == 200) {
                                layer.msg(da.msg)
                                obj.del(); //删除对应行（tr）的DOM结构
                                // location.reload();//刷新当前页面
                            } else {
                                layer.msg(da.msg)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    }) //ajax结束
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                open("文件修改页面", "/view/patient/edit.html?pid=" + data.pid);
            }
        });

        //"用户添加页面",
        function open(title, url) {
            layer.open({
                type: 2
                , title: title
                , content: url
                , offset: '50px'
                , area: ['500px', '60%']
                , end: function () {
                    table.reload('testReload');

                }
            });
        }

        function searchBtn() {

        }

        var $ = layui.$, active = {
            reload: function () {
                var hid = $('#hid');
                var pname = $('#pname');
                var pphone = $('#pphone');
                var puser = $('#puser');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "hid": hid.val(),
                        "pname": pname.val(),
                        "pphone": pphone.val(),
                        "puser": puser.val()
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>